import { message } from 'antd';
import axios from 'axios';
import history from './history';
import { Gettoken, Removtoken } from './localStorage';
export const baseURL = 'http://geek.itheima.net/v1_0';
const Reaquest = axios.create({
  baseURL: baseURL,
  timeout: 5000,
});

// 请求拦截器
Reaquest.interceptors.request.use(
  function (config) {
    if (Gettoken()) {
      config.headers.Authorization = `Bearer ${Gettoken()}`;
    }
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);

// 响应拦截器
Reaquest.interceptors.response.use(
  function (response) {
    return response.data;
  },
  function (error) {
    if (error.response.status === 401) {
      message.warning('Token身份失效,请重新登录!');
      history.push('/login');
      Removtoken();
    }
    console.dir(error.response.data.message);
    return Promise.reject(error);
  }
);

export default Reaquest;
